<template>
  <div style="padding: 10px">
    <!--    功能区域-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="addMember">新增会员</el-button>
      <el-popconfirm style="margin: 10px 10px" title="确定删除吗？" @onConfirm="selectDelete">
        <template #reference>
          <el-button type="primary">删除选中</el-button>
        </template>
      </el-popconfirm>
      <el-button type="primary" @click="exportExcel">导出Excel</el-button>
    </div>
    <!--    搜索区域-->
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入关键字查询" style="width: 40%;background-color: #fff">
        <el-select slot="prepend" v-model="select" placeholder="请选择" style="width: 120px">
          <el-option label="会员名称" value="memberName" />
          <el-option label="手机号" value="phoneNumber" />
        </el-select>
        <el-button slot="append" icon="el-icon-search" @click="inquire" />
      </el-input>
    </div>
    <!--    展示选中会员信息-->
    <el-table
      v-show="memberShowData.length"
      ref="multipleTable"
      :data="memberShowData"
      tooltip-effect="dark"
      style="width: 100%"
      border
      stripe
      :header-cell-style="headClass"
      :cell-style="rowClass"
    >
      <el-table-column
        prop="memberName"
        label="会员名称"
      />
      <el-table-column
        prop="phoneNumber"
        label="手机号"
      />
      <el-table-column
        prop="birthday"
        label="生日"
        :formatter="formatDate"
      />
      <el-table-column
        prop="balance"
        label="余额"
      />
      <el-table-column
        prop="remark"
        label="备注"
      />
      <el-table-column label="操作" width="140">
        <template #default="scope">
          <el-button type="primary" size="mini" @click="handleAdd(scope.row)">充值</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    充值明细展示-->
    <RechargeShowTable
      ref="RechargeShowTable"
    />
    <!--    选择充值会员区域-->
    <!--    选择会员弹窗-->
    <el-dialog
      title="请选择会员"
      :visible.sync="selectRechargeVisible"
      width="80%"
      center
      :show-close="false"
    >
      <!--    会员表格区域-->
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        :header-cell-style="headClass"
        :cell-style="rowClass"
      >
        <el-table-column
          label="序号"
          type="index"
          :index="indexMethod"
          sortable
        />
        <el-table-column
          prop="memberName"
          label="会员名称"
        />
        <el-table-column
          prop="phoneNumber"
          label="手机号"
        />
        <el-table-column
          prop="birthday"
          label="生日"
          :formatter="formatDate"
          sortable
        />
        <el-table-column
          prop="balance"
          label="余额"
          sortable
        />
        <el-table-column
          prop="remark"
          label="备注"
        />
        <el-table-column label="操作" width="140">
          <template #default="scope">
            <el-button type="primary" size="mini" @click="handleSelect(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--    会员分页-->
      <div style="margin: 10px 0">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-dialog>
    <!--    增加会员弹窗-->
    <el-dialog
      title="添加会员"
      :visible.sync="addMemberVisible"
      width="30%"
      center
      :show-close="false"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item
          label="会员名称"
          prop="memberName"
          :rules="{required: true, message: '会员名称不能为空', trigger: 'blur'}"
        >
          <el-input v-model="form.memberName" style="width: 80%" />
        </el-form-item>
        <el-form-item
          label="手机号"
          prop="phoneNumber"
        >
          <el-input v-model="form.phoneNumber" type="phoneNumber" autocomplete="off" style="width: 80%" />
        </el-form-item>
        <el-form-item
          label="生日"
          prop="birthday"
        >
          <el-date-picker
            v-model="form.birthday"
            style="width: 80%"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            :editable="true"
          />
        </el-form-item>
        <el-form-item
          label="备注"
          prop="remark"
        >
          <el-input v-model="form.remark" type="textarea" :rows="3" style="width: 80%" />
        </el-form-item>
        <div style="margin-top: 50px;text-align: center">
          <el-button style="margin-right: 20px" size="big" @click="cancel">取 消</el-button>
          <el-button style="margin-left: 20px" size="big" type="primary" @click="submitForm('form')">确 定</el-button>
        </div>
      </el-form>

    </el-dialog>
    <!--    充值弹窗-->
    <el-dialog
      :visible.sync="addMoneyVisible"
      width="30%"
      center
      :show-close="false"
    >
      <span slot="title" style="font-weight: bolder;color: goldenrod">正在为:【{{ addMoneyTitle }}】进行充值</span>
      <el-form ref="addMoneyForm" :model="addMoneyForm" label-width="120px">
        <el-form-item
          label="充值金额"
          prop="money"
          :rules="{required: true, message: '充值金额不能为空', trigger: 'blur'}"
        >
          <el-input v-model="addMoneyForm.money" type="number" style="width: 80%" />
        </el-form-item>
        <el-form-item
          label="备注"
          prop="remark"
        >
          <el-input v-model="addMoneyForm.remark" type="textarea" :rows="3" style="width: 80%" />
        </el-form-item>
        <div style="margin-top: 50px;text-align: center">
          <el-button style="margin-right: 20px" size="big" @click="cancelByAddMoneyForm">取 消</el-button>
          <el-button style="margin-left: 20px" size="big" type="primary" @click="submitByAddMoneyForm('addMoneyForm')">确 定</el-button>
        </div>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>

import { Message } from '_element-ui@2.13.2@element-ui'
import { addMember, selectMember } from '@/api/registerMember'
import { addMoney } from '@/api/recharge'
import RechargeShowTable from '@/components/RechargeShowTable'
export default {
  name: 'Recharge',
  components: {
    RechargeShowTable
  },
  data() {
    return {
      // 会员信息
      member: '',
      // 充值弹窗标题
      addMoneyTitle: '',
      // 充值弹窗表数据
      addMoneyForm: {},
      // 充值弹窗
      addMoneyVisible: false,
      // 会员信息展示表
      memberShowData: [],
      // 选择会员弹窗
      selectRechargeVisible: false,
      // 表单校验
      rules: {
        phoneNumber: [
          {
            required: true, message: '请输入联系方式', trigger: 'blur' },
          {
            min: 11, max: 11, message: '请输入11位手机号码', trigger: 'blur' },
          {
            pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
            message: '请输入正确的手机号码'
          }
        ]
      },
      // 添加会员表格
      form: {},
      // 添加会员弹窗,默认关闭
      addMemberVisible: false,
      // 每页查看数量
      pageSizes: [10, 20, 50, 200],
      // 每页显示数量
      pageSize: 10,
      // 总数量
      total: 0,
      // 当前页
      currentPage: 1,
      // 查询输入框
      search: '',
      // 查询选择项目
      select: 'memberName',
      // 表格数据
      tableData: [],
      // 选中数据
      multipleSelection: []
    }
  },
  methods: {
    // 充值弹窗确认
    submitByAddMoneyForm(addMoneyForm) {
      const data = { recharge: this.addMoneyForm, id: this.member.id }
      // 发送请求
      this.$refs[addMoneyForm].validate((valid) => {
        if (valid) {
          new Promise((resolve, reject) => {
            addMoney(data).then(response => {
              if (response.code === 20000) {
                Message({
                  message: '充值成功!',
                  type: 'success',
                  duration: 3 * 1000
                })
              }
              // 刷新数据页面
              this.addMoneyVisible = false
              this.addMoneyForm = {}
              this.$refs.RechargeShowTable.select(this.member.id)
              new Promise((resolve, reject) => {
                // 查询请求
                selectMember(this.currentPage, this.pageSize, this.member.memberName, 'memberName').then(res => {
                  // 赋值给页面渲染
                  this.memberShowData = res.data.records
                  resolve()
                }).catch(error => {
                  reject(error)
                })
              })
              resolve()
            }).catch(error => {
              reject(error)
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 充值弹窗取消
    cancelByAddMoneyForm() {
      this.addMoneyVisible = false
      this.addMoneyForm = {}
    },
    // 点击充值
    handleAdd(row) {
      this.addMoneyTitle = row.memberName
      this.addMoneyVisible = true
      // 给id赋值方便充值时携带给后台
      this.member = row
      // 刷新
      this.$refs.RechargeShowTable.select(row.id)
    },
    // 页码跟序号处理 工具
    indexMethod(index) {
      const currentPage = this.currentPage // 当前页码
      const pageSize = this.pageSize // 每页条数
      return index + 1 + (currentPage - 1) * pageSize // 返回表格序号
    },
    // 日期处理 工具
    formatDate(row, column) {
      // 获取单元格数据
      const data = row[column.property]
      if (data == null) {
        return null
      }
      const dt = new Date(data)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate()
    },
    // 点击查询
    inquire() {
      // 调用查询方法
      this.selectPage(this.currentPage, this.pageSize)
      // 打开弹窗
      this.selectRechargeVisible = true
    },
    // 查询代码抽取 工具
    selectPage(currentPage, pageSize) {
      new Promise((resolve, reject) => {
        // 查询请求
        selectMember(currentPage, pageSize, this.search, this.select).then(res => {
          // 赋值给页面渲染
          this.tableData = res.data.records
          // 赋值总数
          this.total = res.data.total
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 会员新增取消
    cancel() {
      this.addMemberVisible = false
      this.form = {}
    },
    // 会员弹窗提交
    submitForm(form) {
      // 会员新增
      this.$refs[form].validate((valid) => {
        if (valid) {
          new Promise((resolve, reject) => {
            addMember(this.form).then(response => {
              if (response.code === 20000) {
                Message({
                  message: '添加成功!',
                  type: 'success',
                  duration: 3 * 1000
                })
              }
              // 刷新数据页面
              this.selectPage(this.currentPage, this.pageSize)
              // 关闭新增窗口
              this.addMemberVisible = false
              // 清空上传组件数据
              this.form = {}
              resolve()
            }).catch(error => {
              reject(error)
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 点击新增按钮
    addMember() {
      // 打开新增视图
      this.addMemberVisible = true
    },
    // 改变每页显示条数触发
    handleSizeChange(val) {
      this.pageSize = val
      this.selectPage(this.currentPage, val)
    },
    // 改变当前页码触发
    handleCurrentChange(val) {
      this.currentPage = val
      this.selectPage(val, this.pageSize)
    },
    // 表头居中显示
    headClass() {
      return 'text-align:center'
    },
    // 表格数据居中显示
    rowClass() {
      return 'text-align:center'
    },
    // 选择会员弹窗,选中按钮
    handleSelect(row) {
      // 打开新增窗口,改变为编辑
      this.selectRechargeVisible = false
      // 赋值查看
      this.memberShowData = [row]
      // 给id赋值方便充值时携带给后台
      this.member = row
      // 调用子组件方法
      this.$refs.RechargeShowTable.select(row.id)
    },
    selectDelete() {
      // 删除选中
      this.$refs.RechargeShowTable.selectDelete()
    },
    exportExcel() {
      this.$refs.RechargeShowTable.exportExcel(this.member)
    }
  }
}
</script>

<style scoped>
</style>
